package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.dal.ITaskDataDAL;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.BookshelfFavoritesViewModel;
import com.qire.manhua.model.vo.BookshelfFavoriteItemVO;
import com.qire.manhua.model.vo.BookshelfRecommendVO;
import com.qire.manhua.model.vo.BookshelfRecommendVO.RecommendedItemVO;
import com.qire.manhua.model.vo.OkResponseVO;
import com.qire.manhua.model.vo.SignInInfoVO;

import java.util.List;

public class HomeBookshelfFavoritesService extends DataService {

    /**循环一个星期*/
    private final int CYCLE_WEEK = 7;

    private final ITaskDataDAL taskDataDAL = WarehouseFactory.create(this, ITaskDataDAL.class);
    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);

    private BookshelfFavoritesViewModel viewModel;

    public HomeBookshelfFavoritesService(BookshelfFavoritesViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载签到信息
     */
    public void fetchSignInInfo() {
        taskDataDAL.fetchSignInInfo();
    }
    @StrategyAnnotation(event = Url.sign_info)
    public void fetchSignInInfoApiCallback(SignInInfoVO signInResponseVO, boolean isCache) {
        boolean isSignIn        = signInResponseVO.is_sign == 1;
        boolean isFirstSignIn   = signInResponseVO.is_new == 1;
        int todaySignCount      = signInResponseVO.sign_count % CYCLE_WEEK;
        int tomorrowSignCount   = (signInResponseVO.sign_count + 1) % CYCLE_WEEK;
        int todayBeans          = signInResponseVO.init_gold + (todaySignCount * signInResponseVO.accumulate);
        int tomorrowBeans       = signInResponseVO.init_gold + (tomorrowSignCount * signInResponseVO.accumulate);

        viewModel.updateSignInInfo(isSignIn, isFirstSignIn, todayBeans, tomorrowBeans);
        viewModel.bannerAdapterNotifyDataChanged(true, false);
    }

    /**
     * 获取banner位推广漫画列表
     */
    public void fetchBannerList() {
        comicDataDAL.fetchBookshelfRecommendList();
    }
    @StrategyAnnotation(event = Url.shelf_recomm)
    public void fetchBannerListApiCallback(BookshelfRecommendVO dataVO, boolean isCache) {
        if(dataVO == null || dataVO.recomms == null) {
            return ;
        }
        for(RecommendedItemVO vo : dataVO.recomms) {
            viewModel.addBannerItem(new ComicBookEntity(vo.book_id, vo.book_name), vo.recomm_title, vo.recomm_img);
        }
        viewModel.bannerAdapterNotifyDataChanged(false, true);
    }

    /**
     * 获取收藏夹项目列表
     * */
    public void fetchFavoritesList() {
        comicDataDAL.fetchFavoritesList(0);
    }
    @StrategyAnnotation(event = Url.HomeBookshelfFavoritesComicList)
    public void fetchFavoritesListApiCallback(List<BookshelfFavoriteItemVO> dataList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataList)) {
            return;
        }

        for(BookshelfFavoriteItemVO vo : dataList) {
            BookshelfFavoriteItemVO.BookInfoVO bookInfoVO = vo.bookInfo;
            BookshelfFavoriteItemVO.VipTipsVO  vipTipsVO = vo.vipTips;

            ComicBookEntity comicBookEntity = new ComicBookEntity(bookInfoVO.id, bookInfoVO.book_name, vo.last_chapter_id, bookInfoVO.book_unruly);
            boolean isShowTips  = vipTipsVO.show == 1;
            String  vipTipsText = vipTipsVO.text;
            String  vipFreeDate = vipTipsVO.date;

            viewModel.addFavoritesItem(comicBookEntity, isShowTips, vipTipsText, vipFreeDate, vo.last_read_px, vo.last_update_px);
        }

        viewModel.favoritesAdapterNotifyDataChanged();

        viewModel.refreshEmptyDataUI(false);

    }

    /**
     * 提交删除收藏列表数据
     * */
    public void submitDelFavorites(List<String> comicIdList) {
//        viewModel.showDialog("正在删除", true, null);
        comicDataDAL.batchDelFavoritesList(comicIdList);
    }
    @StrategyAnnotation(event = Url.my_user_shelf_delete)
    public void submitDelFavoritesApiCallback(OkResponseVO dataVO, boolean isCache){
//        viewModel.dismissDialog();
        if(ObjectUtil.isNull(dataVO)) {
            ToastHelper.showSnackBar("删除操作失败", Prompt.ERROR);
        }

        if(dataVO.isOK()) {
            viewModel.clearBatchDeleteList();
            viewModel.loadFavoritesData();
            ToastHelper.showSnackBar("删除成功", Prompt.SUCCESS);
        } else {
            ToastHelper.showSnackBar("删除操作失败", Prompt.ERROR);
        }
    }

}
